package des.wangku.operate.standard.utls;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 字符串工具类
 * @author Sunjian
 * @version 1.0
 * @since jdk1.8
 */
public final class UtilsString {
	/** 日志 */
	static Logger logger = LoggerFactory.getLogger(UtilsString.class);

	/**
	 * 把字符串中，所有在字符串末尾的数组中含有的全部清除<br>
	 * ("a.0bc.00.0.000.0.0.0.0",".0",".00",".000") 结果:a.0bc ,
	 * @param source String
	 * @param arrs String[]
	 * @return String
	 */
	public static final String removeEndsStr(String source, String... arrs) {
		if (source == null || source.length() == 0 || arrs.length == 0) return source;
		String key = null;
		for (String e : arrs)
			if (source.endsWith(e)) {
				key = e;
				break;
			}
		if (key == null) return source;
		int index = source.lastIndexOf(key);
		return removeEndsStr(source.substring(0, index), arrs);
	}

	/**
	 * 过滤字符串中的所有中括号
	 * @param word String
	 * @return String
	 */
	public static final String splitWordMiddleBrackets(String word) {
		if (word == null || word.length() == 0) return word;
		return word.replaceAll("\\[[^]]*\\]", "");
	}

	/**
	 * 得到字符串的宽度
	 * @param string String
	 * @param font Font
	 * @return int
	 */
	public static int getStringWidth(String string, Font font) {
		int width = 0;
		Shell shell = new Shell();
		Label label = new Label(shell, SWT.NONE);
		label.setFont(font);
		GC gc = new GC(label);
		for (int i = 0; i < string.length(); i++) {
			char c = string.charAt(i);
			width += gc.getAdvanceWidth(c);
		}
		gc.dispose();
		shell.dispose();
		return width;
	}

	/**
	 * 判断关键字是否含有在数组中，区分大小写
	 * @param key String
	 * @param arrs String[]
	 * @return boolean
	 */
	public static final boolean isExistIndexOf(String key, String... arrs) {
		if (key == null) return false;
		for (String e : arrs)
			if (e != null && e.indexOf(key) > -1) return true;
		return false;
	}

	/**
	 * 判断关键字是否含有Set数组中，区分大小写
	 * @param key String
	 * @param set Set&lt;String&gt;
	 * @return boolean
	 */
	public static final boolean isExistIndexOf(String key, Set<String> set) {
		if (key == null) return false;
		for (String e : set)
			if (e != null && e.indexOf(key) > -1) return true;
		return false;
	}

	/**
	 * 判断关键字是否在数组中，区分大小写
	 * @param key String
	 * @param arrs String[]
	 * @return boolean
	 */
	public static final boolean isExistdef(String key, String... arrs) {
		if (key == null) return false;
		for (String e : arrs)
			if (key.equals(e)) return true;
		return false;
	}

	/**
	 * 判断数组中的多个字符串是否在line中出现(indexof)，区分大小写
	 * @param line String
	 * @param arrs String[]
	 * @return boolean
	 */
	public static final boolean isExistKey(String line, String... arrs) {
		if (line == null) return false;
		for (String e : arrs)
			if (line.indexOf(e) > -1) return true;
		return false;
	}

	/**
	 * 判断字符串中是否含有关键字数组中的元素
	 * @param str String
	 * @param arrs String[]
	 * @return boolean
	 */
	public static final boolean isContain(String str, String... arrs) {
		if (str == null || str.length() == 0) return false;
		for (String key : arrs)
			if (str.indexOf(key) > -1) return true;
		return false;
	}

	/**
	 * 得到字符串第2个以上的位置的下标
	 * @param str String
	 * @param key String
	 * @param point int
	 * @return int
	 */
	public static int getStringPosition(String str, String key, int point) {
		Matcher slashMatcher = Pattern.compile(key).matcher(str); //这里是获取"/"符号的位置
		int mIdx = 0;
		while (slashMatcher.find())
			if ((mIdx++) == point) return slashMatcher.start();
		return -1;
	}

	public static void main2(String[] args) {
		String strInput = "3a7s10@5d2a6s17s56;3553";
		String regEx = "[^0-9]";//匹配指定范围内的数字

		//Pattern是一个正则表达式经编译后的表现模式
		Pattern p = Pattern.compile(regEx);

		// 一个Matcher对象是一个状态机器，它依据Pattern对象做为匹配模式对字符串展开匹配检查。
		Matcher m = p.matcher(strInput);

		//将输入的字符串中非数字部分用空格取代并存入一个字符串
		String string = m.replaceAll(" ").trim();

		//以空格为分割符在讲数字存入一个字符串数组中
		String[] strArr = string.split(" ");

		//遍历数组转换数据类型输出
		for (String s : strArr) {
			System.out.println(Integer.parseInt(s));
		}
		String str = " 11第12页 第28位 第31条  ";
		System.out.println("-----" + getNumbers(str, "第\\d+页"));
		System.out.println("-----" + getNumbers(str, "第\\d+条"));
		System.out.println("-----" + getNumbers(str, "第\\d+位"));

		String title = "6月上新 (40)";
		System.out.println("======" + UtilsString.getNumbersInt(title, "\\(", "\\)"));
		String url = "http://www.sohu.com/abc/def";
		String url2 = "https://www.sohu.com\\abc\\def";
		System.out.println("======" + UtilsReadURL.getUrlDomain(url));
		System.out.println("======" + UtilsReadURL.getUrlDomain(url2));
		System.out.println("======" + UtilsReadURL.getUrlDomain("   "));
		String shortstr = "abcdefghijklmnopqrestuvwxyz123456789";
		String result = getShortenedString(shortstr, 19);
		System.out.println("result:" + result);
	}

	/**
	 * 第50(页/条/位)<br>
	 * getNumbersInt(str, "第","页")<br>
	 * 没有找到，则返回-1
	 * @param content String
	 * @param before String
	 * @param after String
	 * @return int
	 */
	public static int getNumbersInt(String content, String before, String after) {
		return getNumbersInt(content, before + "\\d+" + after);
	}

	/**
	 * 第50(页/条/位)<br>
	 * getNumbers(str, "第\\d+页")<br>
	 * 没有找到，则返回-1
	 * @param content String
	 * @param str String
	 * @return int
	 */
	public static int getNumbersInt(String content, String str) {
		String result = getNumbers(content, str);
		if (result.length() == 0) return -1;
		return Integer.parseInt(result);
	}

	/**
	 * 第50(页/条/位)<br>
	 * getNumbers(str, "第\\d+页")
	 * @param content String
	 * @param str String
	 * @return String
	 */
	public static String getNumbers(String content, String str) {
		Pattern pat = Pattern.compile(str);
		Matcher mat = pat.matcher(content);
		if (!mat.find()) return "";
		String con = mat.group(0);
		Pattern pattern = Pattern.compile("\\d+");
		Matcher matcher = pattern.matcher(con);
		while (matcher.find())
			return matcher.group(0);
		return "";
	}

	/**
	 * 权重中排名的转换 "第1页 第7位"
	 * @param content String
	 * @return int
	 */
	public static int getNumbersIntRankPosid(String content) {
		return getNumbersIntRankPosid(content, "第", "页", "第", "位");
	}

	/**
	 * 权重中排名的转换 "第1页 第7位"<br>
	 * (content,"第","页", "第","位")
	 * @param content String
	 * @param pageFirs String
	 * @param pageEnd String
	 * @param pointFist String
	 * @param pointEnd String
	 * @return int
	 */
	public static int getNumbersIntRankPosid(String content, String pageFirst, String pageEnd, String pointFist, String pointEnd) {
		int sort = 0;
		int page = getNumbersInt(content, pageFirst, pageEnd);
		if (page > 0) sort = (page - 1) * 10;
		int p = getNumbersInt(content, pointFist, pointEnd);
		if (p > 0) sort += p;
		return sort;
	}

	/**
	 * 判断字符串含有多少个字符串，即字符串重复数量
	 * @param content String
	 * @param key String
	 * @return int
	 */
	public static int getCountInnerStr(final String content, final String key) {
		if (content == null || content.length() == 0 || key == null) return 0;
		final Pattern r = Pattern.compile(key);
		return getCountInnerStr(content, r);
	}

	/**
	 * 判断字符串含有多少个字符串，即字符串重复数量
	 * @param content String
	 * @param r Pattern
	 * @return int
	 */
	public static int getCountInnerStr(final String content, Pattern r) {
		if (content == null || content.length() == 0 || r == null) return 0;
		int count = 0;
		final Matcher m = r.matcher(content);
		while (m.find())
			count++;
		return count;
	}

	static final float ACC_CapacityGB = 1024 * 1024 * 1024;//定义GB的计算常量
	static final String ACC_CapacityGBSign = "GB";
	static final float ACC_CapacityMB = 1024 * 1024;//定义MB的计算常量
	static final String ACC_CapacityMBSign = "MB";
	static final float ACC_CapacityKB = 1024;//定义KB的计算常量
	static final String ACC_CapacityKBSign = "KB";

	static final DecimalFormat ACC_DF_Capacity = new DecimalFormat("0.000");//格式化小数

	/**
	 * 把文件长度转成容量单位<br>
	 * 如1000转成，即1.000KB
	 * @param size long
	 * @return String
	 */
	public static final String getCapacitySize(long size) {
		StringBuilder sb = new StringBuilder(20);
		if (size / ACC_CapacityGB >= 1) {
			sb.append(ACC_DF_Capacity.format(size / ACC_CapacityGB));
			sb.append(ACC_CapacityGBSign);
		} else if (size / ACC_CapacityMB >= 1) {
			sb.append(ACC_DF_Capacity.format(size / ACC_CapacityMB));
			sb.append(ACC_CapacityMBSign);
		} else if (size / ACC_CapacityKB >= 1) {
			sb.append(ACC_DF_Capacity.format(size / ACC_CapacityKB));
			sb.append(ACC_CapacityKBSign);
		} else {
			sb.append(size);
			sb.append('B');
		}
		return sb.toString();
	}

	static final DecimalFormat ACC_DF_Sizes = new DecimalFormat("0000");//格式化小数

	/**
	 * 把文件长度转成容量单位<br>
	 * 如1001000转成，即100K1000B
	 * @param size long
	 * @return String
	 */
	public static final String getCapacitySizes(long size) {
		StringBuilder sb = new StringBuilder(30);
		int val;
		if (size / ACC_CapacityGB >= 1) {
			val = (int) (size / ACC_CapacityGB);
			sb.append(ACC_DF_Sizes.format(val));
			sb.append('G');
			size %= ACC_CapacityGB;
		}
		if (size / ACC_CapacityMB >= 1) {
			val = (int) (size / ACC_CapacityMB);
			sb.append(ACC_DF_Sizes.format(val));
			sb.append('M');
			size %= ACC_CapacityMB;
		}
		if (size / ACC_CapacityKB >= 1) {
			val = (int) (size / ACC_CapacityKB);
			sb.append(ACC_DF_Sizes.format(val));
			sb.append('K');
			size %= ACC_CapacityKB;
		}
		sb.append(ACC_DF_Sizes.format(size));
		sb.append('B');
		return sb.toString();
	}

	/**
	 * 判断字符串是否为空，null或过滤两侧空格后仍为空，则为真
	 * @param str String
	 * @return boolean
	 */
	public static final boolean isSpace(String str) {
		if (str == null) return true;
		str = str.trim();
		return str.length() == 0;
	}

	static final char[] SHORTARR = { '.', '.', '.' };

	/**
	 * 把长字符串缩短成指定长度的字符串，中间以...进行省略
	 * @param sourceStr String
	 * @param maxlen int
	 * @return String
	 */
	public static final String getShortenedString(final String sourceStr, int maxlen) {
		if (sourceStr == null) return sourceStr;
		String str = sourceStr.trim();
		if (str.length() == 0 || str.length() <= maxlen) return str;
		char[] arr = new char[maxlen];
		int p = (maxlen - 3) / 2;
		char[] h = str.substring(0, p).toCharArray();
		char[] e = str.substring(str.length() - (maxlen - (h.length + 3)), str.length()).toCharArray();
		System.out.println("p:" + p);
		System.out.println("e:" + e.length);
		System.arraycopy(h, 0, arr, 0, p);
		System.arraycopy(SHORTARR, 0, arr, p, 3);
		System.arraycopy(e, 0, arr, p + 3, e.length);
		return new String(arr);
	}

	/**
	 * 判断字符串是否含有以 interkeys 为间隔的字符数组
	 * @param str String
	 * @param arr String
	 * @param interkeys String
	 * @return boolean
	 */
	public static final boolean isContainSplit(String str, String arr, String... interkeys) {
		if (str == null || str.length() == 0) return false;
		for (String key : interkeys) {
			String keysplit = key;
			if (key.equals("|")) keysplit = "\\|";
			String[] arrs = arr.split(keysplit);
			for (String e : arrs) {
				if (str.indexOf(e) != -1) return true;
			}
		}
		return false;
	}

	/**
	 * 过滤掉小数点右侧数字。保留完整整数
	 * @param str String
	 * @return String
	 */
	public static final String getLeftPoint(String str) {
		if (str == null) return null;
		int index = str.indexOf(".");
		if (index == -1) return str;
		return str.substring(0, index);
	}

	private static final char ACC_ShowStringkeySplit = '\t';

	/**
	 * 多个对象整合成一个字符串，以\t以间隔，可以含有多个数组
	 * @param arrs T[]
	 * @return String
	 */
	@SuppressWarnings("unchecked")
	public static final <T> String showString(T... arrs) {
		StringBuilder sb = new StringBuilder();
		for (T e : arrs) {
			if (e == null) {
				if (sb.length() != 0) sb.append(ACC_ShowStringkeySplit);
				sb.append("null");
				continue;
			}
			if (e.getClass().isArray()) {
				Object[] arr = (Object[]) e;
				sb.append(showString(arr));
			} else {
				if (sb.length() != 0) sb.append(ACC_ShowStringkeySplit);
				sb.append(e.toString());
			}
		}
		return sb.toString();
	}

	/**
	 * 数组的转向
	 * @param arrs T[]
	 * @return T[]
	 */
	public static final <T> T[] reverseOrder(T[] arrs) {
		int len = arrs.length;
		if (len < 2) return arrs;
		for (int i = 0, size = len / 2; i < size; i++) {
			T obj = arrs[i];
			int to = len - 1 - i;
			arrs[i] = arrs[to];
			arrs[to] = obj;
		}
		return arrs;
	}

	/**
	 * 数组按长度从小到大排序
	 * @param arrs String[]
	 * @return String[]
	 */
	public static final String[] sortStringArrayByLenReverse(String... arrs) {
		String[] arr = sortStringArrayByLen(arrs);
		return reverseOrder(arr);
	}

	/**
	 * 数组按长度从大到小排序
	 * @param arrs String[]
	 * @return String[]
	 */
	public static final String[] sortStringArrayByLen(String... arrs) {
		int len = arrs.length;
		if (len <= 1) return arrs;
		for (int i = 0, end = len - 1; i < end; i++) {
			String key = arrs[i];
			int index = getIndexMaxLength(arrs, key == null ? -1 : key.length(), i + 1);
			if (index > -1) {
				arrs[i] = arrs[index];
				arrs[index] = key;
			}
		}
		return arrs;
	}

	/**
	 * 得到数组中长度最长单元的下标
	 * @param arr Object[]
	 * @param maxLen int
	 * @param start int
	 * @return int
	 */
	public static final int getIndexMaxLength(Object[] arr, int maxLen, int start) {
		return getIndexMaxLength(arr, maxLen, start, arr.length - 1);
	}

	/**
	 * 得到数组中长度最长单元的下标
	 * @param arr Object[]
	 * @param maxLen int
	 * @param start int
	 * @param end int
	 * @return int
	 */
	public static final int getIndexMaxLength(Object[] arr, int maxLen, int start, int end) {
		int len = arr.length;
		if (start < 0 || start >= len) start = 0;
		if (end >= len) end = len - 1;
		int index = -1;
		for (int i = start; i <= end; i++) {
			Object v = arr[i];
			if (v == null) continue;
			if (v.toString().length() <= maxLen) continue;
			index = i;
			maxLen = v.toString().length();
		}
		return index;
	}

	/**
	 * 格式化字符串，先判断是否为数字。如果为数字，则前面补0，如果为字符串，则左对齐
	 * @param id String
	 * @param maxlen int
	 * @return String
	 */
	public static final String format(String id, int maxlen) {
		if (UtilsVerification.isNumeric(id)) return String.format("%0" + maxlen + "d", Integer.parseInt(id));
		return String.format("%1$-" + maxlen + "s", id);
	}

	/**
	 * 格式化正整数<br>
	 * "5,4"输出0005
	 * @param val int
	 * @param size int
	 * @return String
	 */
	static final String formatNumber(int val, int size) {
		if (val < 0) return val + "";
		String s = String.valueOf(val);
		if (s.length() >= size) return s;
		char[] arr = s.toCharArray();
		char[] newarr = new char[size];
		int start = size - s.length();
		for (int i = 0; i < start; i++)
			newarr[i] = '0';
		System.arraycopy(arr, 0, newarr, start, s.length());
		return new String(newarr);
	}

	/**
	 * 识别[]之间的数据，并得到数组
	 * @param key String
	 * @return String[]
	 */
	static final String[] keySplit(String key) {
		List<String> list = new ArrayList<>();
		String[] arr = {};
		String val = key.substring(1, key.length() - 1);
		if (val.length() == 0) return arr;
		String[] keys = val.split(",");
		for (String e : keys) {
			int index = e.indexOf('-');
			if (index == 0) continue;
			if (index == -1) {
				list.add(e);
				continue;
			}
			String[] cuts = e.split("-");
			String c1 = cuts[0];
			String c2 = cuts[1];
			/* 数字之间进行泛型组合 */
			if (UtilsVerification.isNumeric(c1) && UtilsVerification.isNumeric(c2)) {
				int first = Integer.valueOf(c1);
				int end = Integer.valueOf(c2);
				boolean isFormat = false;/* 数字格式化 */
				if (c1.length() == c2.length()) isFormat = true;
				if (first > end) {
					for (int i = first; i >= end; i--)
						list.add(isFormat ? formatNumber(i, c1.length()) : "" + i);
				} else {
					for (int i = first; i <= end; i++)
						list.add(isFormat ? formatNumber(i, c1.length()) : "" + i);
				}
				continue;
			}
			/* 字符之间进行泛型组合 */
			if (c1.length() == 1 && c2.length() == 1) {
				char first = c1.charAt(0);
				char end = c2.charAt(0);
				if (first > end) {
					for (int i = first; i >= end; i--)
						list.add("" + (char) i);
				} else {
					for (int i = first; i <= end; i++)
						list.add("" + (char) i);
				}
			}
		}
		list = UtilsList.distinct(list);/* 去重 */
		return list.toArray(arr);
	}

	/**
	 * 数字之间的泛型 允许格式化<br>
	 * 如"001-005" 结果为[001,002,003,004,005]<br>
	 * 允许前后大小互换
	 * @param key String
	 * @return String[]
	 */
	public static final String[] getGenericityInteger(String key) {
		String[] arr = {};
		if (key == null || key.indexOf("-") == -1) return arr;
		String[] arrs = key.split("-");
		String c1 = arrs[0];
		String c2 = "";
		if (arrs.length > 1) c2 = arrs[1];
		return getGenericityInteger(c1, c2);
	}

	/**
	 * 数字之间的泛型 允许格式化<br>
	 * 如"001-005" 结果为[001,002,003,004,005]<br>
	 * 允许前后大小互换
	 * @param c1 String
	 * @param c2 String
	 * @return String[]
	 */
	public static final String[] getGenericityInteger(String c1, String c2) {
		String[] arr = {};
		if (c1 == null || c2 == null) return arr;
		c1 = c1.trim();
		c2 = c2.trim();
		if (c1.length() == 0 && c2.length() == 0) return arr;
		int n1 = 0, n2 = 0;
		boolean isFormatNum = false;/* 数字格式化 */
		if (c1.length() == 0) {
			n1 = 0;
		} else if (UtilsVerification.isNumeric(c1)) {
			n1 = Integer.valueOf(c1);
		} else {
			return arr;
		}
		if (c2.length() == 0) {
			n2 = 0;
		} else if (UtilsVerification.isNumeric(c2)) {
			n2 = Integer.valueOf(c2);
		} else {
			return arr;
		}
		int maxSize = 0;
		if (n1 != 0 && c1.charAt(0) == '0') {
			isFormatNum = true;
			maxSize = c1.length();
		}
		if (n2 != 0 && c2.charAt(0) == '0') {
			isFormatNum = true;
			if (c2.length() > maxSize) maxSize = c2.length();
		}
		System.out.println("[" + c1 + "-" + c2 + "]:" + isFormatNum);
		List<String> list = new ArrayList<>();
		if (n1 > n2) {
			for (int i = n1; i >= n2; i--)
				list.add(isFormatNum ? formatNumber(i, maxSize) : "" + i);
		} else {
			for (int i = n1; i <= n2; i++)
				list.add(isFormatNum ? formatNumber(i, maxSize) : "" + i);
		}
		return list.toArray(arr);
	}

	/**
	 * 把字符串http://www.books.net/fenlei/[a-c]_[a,1-2].html转成不同组合
	 * @param str String
	 * @return String[]
	 */
	public static final String[] splitString(String str) {
		String[] arr = {};
		if (str == null || str.trim().length() == 0) return arr;
		String newstr = str.trim();
		List<String> list = new ArrayList<>();
		combinationString(list, newstr);
		list = UtilsList.distinct(list);/* 去重 */
		return list.toArray(arr);
	}

	/** 正则 间括号 */
	static final String ACC_Combination = "\\[[^]]*\\]";

	/**
	 * 把字符串http://www.books.net/fenlei/[a-c]_[a,1-2].html转成不同组合并放在List中<br>
	 * 使用递归调用<br>
	 * @param list List&lt;String&gt;
	 * @param str String
	 */
	private static final void combinationString(List<String> list, String str) {
		final Pattern r = Pattern.compile(ACC_Combination);
		final Matcher m = r.matcher(str);
		if (!m.find()) {
			list.add(str);
			return;
		}
		String key = m.group();
		String[] arr = keySplit(key);
		for (String e : arr) {
			String newString = m.replaceFirst(e);
			combinationString(list, newString);
		}
	}

	/**
	 * 得到key字符串在content中的重复率，即content中有多少key中的字符<br>
	 * 如返回80，则key6中有百分之80的字符出现在content中
	 * @param content String
	 * @param key String
	 * @return String
	 */
	public static final int repetitionRate(String content, String key) {
		if (content == null || content.length() == 0) return 0;
		if (key == null || key.length() == 0) return 0;
		int len = key.length();
		int sort = 0;
		for (int i = 0; i < len; i++) {
			char c = key.charAt(i);
			if (content.indexOf(c) > -1) sort++;
		}
		float v = (float) sort / (float) len * 100;
		//System.out.println(sort+"="+len+"\t"+v);
		return (int) (v);
	}

	/**
	 * 截断字符串，如果没有找到，则返回null，如果找到多个，则返回第1个
	 * @param source String
	 * @param start String
	 * @param end String
	 * @return String
	 */
	public static final String cutString(String source, String start, String end) {
		String[] arr = cutStrings(source, start, end);
		if (arr.length == 0) return null;
		return arr[0];
	}

	/**
	 * 截取字符串，产生多个字符串，为开始与结尾之间的字符串<br>
	 * 如果开始为空串，则以最始点为开始<br>
	 * 如果结尾为空串，则以最后点为结尾
	 * @param source String
	 * @param start String
	 * @param end String
	 * @return String[]
	 */
	public static final String[] cutStrings(String source, String start, String end) {
		String[] arrs = {};
		if (source == null || source.length() == 0) return arrs;
		if (start == null || end == null) return arrs;
		if (start.length() == 0 && end.length() == 0) return arrs;
		String cut = null;
		if (start.length() == 0) {
			int index = source.indexOf(end);
			if (index == -1) return arrs;
			cut = source.substring(0, index);
		}
		if (end.length() == 0) {
			int index = source.indexOf(start);
			if (index == -1) return arrs;
			cut = source.substring(index + start.length(), source.length());
		}
		if (cut != null) {
			String[] a = { cut };
			return a;
		}
		int index1 = 0, index2 = 0;
		List<String> list = new ArrayList<>();
		while ((index1 = source.indexOf(start, index1)) > -1) {
			index2 = source.indexOf(end, index1);
			if (index2 == -1) break;
			cut = source.substring(index1 + start.length(), index2);
			int lastStart = cut.lastIndexOf(start);
			if (lastStart > -1) cut = cut.substring(lastStart + start.length());
			list.add(cut);
			index1 = index2 + end.length();
		}
		if (list.size() == 0) return arrs;
		return list.toArray(arrs);
	}

	public static String removeStr(String src, String str) {
		if (src == null || str == null) return src;
		int idx = src.indexOf(str);
		if (idx == -1) return src;
		int pst = 0;
		char[] cs = src.toCharArray();
		char[] rs = new char[src.length() - str.length()];
		for (int i = 0; i < cs.length; i++) {
			if (i >= idx && i < idx + str.length()) continue;
			rs[pst] = cs[i];
			pst++;
		}
		return new String(rs);
	}

	public static String replaceStr(String src, String target, String replacement) {
		if (src == null || target == null || replacement == null) return src;
		int idx = src.indexOf(target);
		if (idx == -1) return src;
		int pst = 0;
		char[] cs = src.toCharArray();
		char[] rs = new char[src.length() - target.length() + replacement.length()];
		for (int i = 0; i < cs.length; i++) {
			if (i == idx) {
				for (char c : replacement.toCharArray()) {
					rs[pst] = c;
					pst++;
				}
				continue;
			}
			if (i > idx && i < idx + target.length()) continue;
			rs[pst] = cs[i];
			pst++;
		}
		return new String(rs);
	}

	/**
	 * @param src
	 * @param target
	 * @param replacement
	 * @return
	 */
	public static String replaceAllStr(String src, String target, String replacement) {
		if (src == null || target == null || replacement == null) return src;
		int idx = src.indexOf(target);
		if (idx == -1) return src;
		int pst = 0;
		char[] cs = src.toCharArray();
		char[] rs = new char[src.length() - target.length() + replacement.length()];
		for (int i = 0; i < cs.length; i++) {
			if (i == idx) {
				for (char c : replacement.toCharArray()) {
					rs[pst] = c;
					pst++;
				}
				continue;
			}
			if (i > idx && i < idx + target.length()) continue;
			rs[pst] = cs[i];
			pst++;
		}
		return replaceAllStr(new String(rs), target, replacement);
	}

	/**
	 * 找到指定位置的的字符串，替换成新的字符串<br>
	 * 不区分大小写
	 * @param content String
	 * @param key String
	 * @param rep String
	 * @param arr int[]
	 * @return String
	 */
	public static final String replacePointStr(String content, String key, String rep, int... arr) {
		if (content == null || content.length() == 0) return content;
		if (key == null || key.length() == 0) return content;
		StringBuilder sb = new StringBuilder(content.length());
		Pattern r = Pattern.compile(key, Pattern.CASE_INSENSITIVE);
		String[] arrs = r.split(content);
		for (int i = 0; i < arrs.length; i++) {
			String e = arrs[i];
			sb.append(e);
			if (UtilsArrays.isExist(i, arr)) if (rep != null) sb.append(rep);
			else sb.append(key);
		}
		return sb.toString();
	}

	/**
	 * 得到字符串中含有的域名<br>
	 * http://xxx.com/abc<br>
	 * www.abc.com
	 * @param content
	 * @return
	 */
	public static final String[] getContainWebsite(String content) {
		String[] arr = {};
		if (content == null || content.length() == 0) return arr;
		List<String> list = new ArrayList<>();
		StringBuilder sb = new StringBuilder();
		String regex = "^((https|http|ftp|rtsp|mms)?://)" + "?(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?" //ftp的user@  
				+ "(([0-9]{1,3}\\.){3}[0-9]{1,3}" // IP形式的URL- 199.194.52.184  
				+ "|" // 允许IP和DOMAIN（域名） 
				+ "([0-9a-z_!~*'()-]+\\.)*" // 域名- www.  
				+ "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\\." // 二级域名  
				+ "[a-z]{2,6})" // first level domain- .com or .museum  
				+ "(:[0-9]{1,4})?" // 端口- :80  
				+ "((/?)|" // a slash isn't required if there is no file name  
				+ "(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$";
		sb.append("(");

		sb.append("(");
		sb.append("(https?|ftp|file)://[-A-Z0-9+&@#/%?=~_|!:,.;]*[-A-Z0-9+&@#/%=~_|]");
		sb.append(")");

		sb.append("|");

		sb.append("(");
		sb.append("[-A-Z0-9.]*\\.(com|cn|net|org|biz|info|cc|tv)");
		sb.append(")");

		sb.append("|");

		sb.append("(");
		sb.append(regex);
		sb.append(")");

		sb.append(")+");
		Pattern p = Pattern.compile(sb.toString(), Pattern.CASE_INSENSITIVE);
		Matcher m = p.matcher(content);
		while (m.find())
			list.add((m.group()));
		return list.toArray(arr);
	}

	public static void main(String[] args) {
		String[] arrs = { "1-5", "-4", "-12", "001-003", "-002", "003-001", "001-" };
		for (String e : arrs) {
			String[] vals = getGenericityInteger(e);
			if (vals.length > 0) System.out.println(e + "=\t" + showString(vals));
		}
		boolean t = false;
		if (t) {
			String content = "<br>aa<b>bb<b>c<br>c</b>d<br>d<b>e<br>e</b>f<br>f<b>xx<b>h<br>h</b>g<br>g";
			System.out.println(content);
			System.out.println("" + replacePointStr(content, "<br>", "<bbr>", 1, 2, 5));
			String[] arr = cutStrings(content, "<b>", "</b>");
			for (String e : arr) {
				System.out.println("e:" + e);
			}
			String str = "abcd";
			System.out.println("result:" + str.substring(2, 4));

			{
				String url = "中aaaaanotherbug.blog.chinajavaworld.com/entry/4545/0/";
				Pattern p = Pattern.compile("[-A-Z0-9+&@#/%?=~_|!:,.;]*\\.(com|cn|net|org|biz|info|cc|tv)", Pattern.CASE_INSENSITIVE);
				Matcher matcher = p.matcher(url);
				matcher.find();
				System.out.println(matcher.group());

			}

			{
				String url = "aaanotherbug.blog.chinajavaworld.com/entry/4545/0/";
				Pattern p = Pattern.compile("(http://|https://|\\.)[^.]*?\\.(com|cn|net|org|biz|info|cc|tv)", Pattern.CASE_INSENSITIVE);
				Matcher matcher = p.matcher(url);
				matcher.find();
				System.out.println(matcher.group());

			}

			{
				String url = "aeehttp://anotherbug.blog.chinajavaworld.com/entry/4545/0/eee,中ww" + "w.xxx.com中国?xxx.com.cn,!www.abc.org!!" + "aa汉ftp://abc.com.cn/zbc/aa.rar#a?2=3离";
				Pattern p = Pattern.compile("(https?|ftp|file)://[-A-Z0-9+&@#/%?=~_|!:,.;]*[-A-Z0-9+&@#/%=~_|]", Pattern.CASE_INSENSITIVE);
				Matcher matcher = p.matcher(url);
				matcher.find();
				System.out.println(matcher.group());
				String[] arrs22 = getContainWebsite(url);
				for (String e : arrs22)
					System.out.println("::" + e);

			}
		}
		/*
		 * String content = "abcaa" + ACC_ENTER + "bbccaaee" + ACC_ENTER + "aaaaa";
		 * String val = ACC_ENTER;
		 * System.out.println("count:" + UtilsRegular.getPatternCount(content, val));
		 */
		/*
		 * String content="aa<font>bb<font>cc</font>dd</font>";
		 * String str=cutString(content,"<font>","</font>");
		 * System.out.println("::"+str);
		 * content="aabbccdeabefhjuabcdefzklaccggkfg";
		 * String key="abcdefzklaccgg";
		 * System.out.println("repetitionRate:"+repetitionRate(content,key));
		 */
		/*
		 * String s ="\\u79fb\\u52a8\\u4e92\\u8054&gt;&amp;\\u7f51\\u5e94\\u7528&ldquo;";
		 * System.out.println(s);
		 * System.out.println(StringEscapeUtils.unescapeJava(s));
		 * System.out.println(StringEscapeUtils.unescapeHtml3(s));
		 * System.out.println(StringEscapeUtils.unescapeHtml4(s));
		 * String str = "http://www.17books.net/fenlei/[a,009-100].html";
		 * String[] arrs = splitString(str);
		 * for (int i = 0; i < arrs.length; i++)
		 * System.out.println(i + ":" + arrs[i]);
		 * String[] ar=keySplit("[110,a-b,c,2-4,30-1,c-a]");
		 * for(int i=0;i<ar.length;i++)
		 * System.out.println(i+":"+ar[i]);
		 */

		/*
		 * String[] arrs=UtilsConstsRequestHeader.User_Agent;
		 * String[] arr=sortStringArrayByLenReverse(arrs);
		 * for(String e:arr) {
		 * System.out.println("\t\t\""+e+"\",");
		 * }
		 * Object[] ar= {"ee",null};
		 * Object[] arr= {5,"abc",ar,'c',"txt",null,15.2,10};
		 * System.out.println(showString(arr));
		 * String[] arrs= {null,"abc",null,"00",null,"1234","5566788","0","abcdef"};
		 * System.out.println(showString(arrs));
		 * String[] arrsa=sortStringArrayByLen(arrs);
		 * System.out.println(showString(arrsa));
		 * System.out.println();
		 * String[] arrs2=sortStringArrayByLenReverse(arrs);
		 * System.out.println(showString(arrs2));
		 * String path = System.getProperty("java.library.path");
		 * System.out.println(path);
		 * Map<String,String> map=System.getenv();
		 * for (Map.Entry<String, String> entry : map.entrySet()) {
		 * System.out.println("map:Key = " + entry.getKey() + ", Value = " + entry.getValue());
		 * }
		 * Properties properties=System.getProperties();
		 * Set<Object> keys = properties.keySet();//返回属性key的集合
		 * for (Object key : keys) {
		 * System.out.println("properties:"+key.toString() + "=" + properties.get(key));
		 * }
		 */
		son sss = new son();
		sss.test();

	}

	static class person {
		String name = "爸爸";

		void test() {
			System.out.println("我是爸爸");
		}
	}

	static final class son extends person {
		String name = "儿子";

		void test() {
			System.out.println("我是儿子");
			super.test();
			System.out.println("我是儿子");
		}
	}
}
